package indi.luckly.service.impl;

import indi.luckly.bean.entity.UserEntity;
import indi.luckly.common.key.ErrorKey;
import indi.luckly.common.request.UpDateUserInfoParams;
import indi.luckly.common.response.QueryUserBaseInfoResponseData;
import indi.luckly.common.response.ResponseResult;
import indi.luckly.mapper.IUserMapper;
import indi.luckly.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    IUserMapper mUserMapper;

    @Override
    public ResponseResult<QueryUserBaseInfoResponseData> queryUserInfo(String userNumber) {
        Map<String,Object> map=new HashMap<>();
        map.put("userNumber",userNumber);
        final List<UserEntity> userEntities = mUserMapper.selectByMap(map);
        QueryUserBaseInfoResponseData data=new QueryUserBaseInfoResponseData();
        if (userEntities!=null&&userEntities.size()>0){
            final UserEntity userEntity = userEntities.get(0);
            BeanUtils.copyProperties(userEntity,data);
            return ResponseResult.ok(data);
        }else {
            return  ResponseResult.fail(ErrorKey.USER_NON_EXISTENT,data);
        }
    }

    @Override
    public ResponseResult<?> upDateUserInfo(UpDateUserInfoParams params) {

        if (params==null){//参数缺失
            return ResponseResult.fail(ErrorKey.PARAMS_DELETION);
        }
        UserEntity userEntity=new UserEntity();
        BeanUtils.copyProperties(params,userEntity);
        final int i = mUserMapper.updateById(userEntity);
        if (i>0){
            return ResponseResult.ok();
        }else {
            return ResponseResult.fail(ErrorKey.UPDATE_USER_INFO_ERROR);
        }

    }
}
